<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
    <title>学生账号管理</title>
    <style>
        body { background: #23272e; font-family: 'Microsoft YaHei', Arial, sans-serif; }
        .sidebar {
            position: fixed;
            top: 0; left: 0; bottom: 0;
            width: 200px;
            background: #181a20;
            padding-top: 60px;
            box-shadow: 2px 0 8px rgba(0,0,0,0.15);
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .sidebar ul {
            list-style: none; padding: 0; margin: 0; width: 100%;
        }
        .sidebar li {
            width: 100%;
        }
        .sidebar a {
            display: block;
            color: #e0e0e0;
            text-decoration: none;
            font-size: 18px;
            padding: 18px 0 18px 40px;
            transition: background 0.2s, color 0.2s;
        }
        .sidebar a:hover, .sidebar a.active {
            background: #2d313a;
            color: #4fc3f7;
        }
        .sidebar .logout {
            color: #ff8a65;
            margin-top: 40px;
            padding-left: 40px;
        }
        .container {
            width: 900px;
            margin: 60px auto 0 240px; /* Adjusted margin for sidebar */
            background: #181a20; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.3); padding: 40px; color: #b0b8c1; }
        h2 { margin-bottom: 30px; color: #fff; text-align: center; }
        .search-container {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            justify-content: center;
        }
        .search-container input[type="text"] {
            flex-grow: 1;
            padding: 10px;
            border: 1px solid #2d313a;
            border-radius: 4px;
            background: #2d313a;
            color: #fff;
            box-sizing: border-box;
        }
        .search-container button {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            background: #4fc3f7;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s ease;
        }
        .search-container button:hover {
            background: #67daff;
        }
        .btn-add-student {
            background: #43a047;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s ease;
            margin-left: auto; /* Pushes button to the right */
        }
        .btn-add-student:hover {
            background: #5cb85c;
        }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #2d313a; padding: 10px; text-align: center; }
        th { background: #2d313a; color: #4fc3f7; }
        tr:nth-child(even) { background: #23272e; }
        .btn-edit {
            background-color: #007bff;
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-edit:hover {
            background-color: #0056b3;
        }
        .btn-delete {
            background-color: #dc3545;
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-delete:hover {
            background-color: #c82333;
        }
        .message { margin-top: 20px; padding: 10px; border-radius: 5px; text-align: center; }
        .success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
    </style>
</head>
<body>
<div class="sidebar">
    <ul>
        <li><a href="guanliyuan_index.jsp">首页</a></li>
        <li><a href="guanliyuan_student_manage" class="active">学生账号管理</a></li>
        <li><a href="guanliyuan_teacher_manage">老师账号管理</a></li>
        <li><a href="guanliyuan_publish_course">发布课程</a></li>
        <li><a href="guanliyuan_course_manage">管理课程</a></li>
    </ul>
    <a href="logout" class="logout">退出登录</a>
</div>
<div class="container">
    <h2>学生账号管理</h2>
    <div class="search-container">
        <input type="text" id="studentIdSearch" name="studentId" placeholder="按学号搜索">
        <input type="text" id="studentNameSearch" name="studentName" placeholder="按姓名搜索">
        <input type="text" id="classNameSearch" name="className" placeholder="按班级搜索">
        <button type="button" onclick="performSearch()">搜索</button>
        <button type="button" class="btn-add-student" onclick="location.href='guanliyuan_add_student.jsp'">添加学生</button>
    </div>

    <c:if test="${not empty errorMessage}">
        <p class="message error"><c:out value="${errorMessage}"/></p>
    </c:if>
    <c:if test="${not empty successMessage}">
        <p class="message success"><c:out value="${successMessage}"/></p>
    </c:if>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>学号</th>
                <th>用户名</th>
                <th>姓名</th>
                <th>性别</th>
                <th>班级</th>
                <th>邮箱</th>
                <th>电话</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="student" items="${students}">
                <tr>
                    <td><c:out value="${student.id}"/></td>
                    <td><c:out value="${student.studentId}"/></td>
                    <td><c:out value="${student.username}"/></td>
                    <td><c:out value="${student.name}"/></td>
                    <td><c:out value="${student.gender}"/></td>
                    <td><c:out value="${student.className}"/></td>
                    <td><c:out value="${student.email}"/></td>
                    <td><c:out value="${student.phone}"/></td>
                    <td>
                        <button type="button" class="btn-edit" onclick="location.href='xuesheng_edit_student?id=${student.id}'">编辑</button>
                        <form action="guanliyuan_student_manage" method="post" onsubmit="return confirm('确定删除该学生账号吗？');" style="display:inline-block;">
                            <input type="hidden" name="action" value="delete">
                            <input type="hidden" name="id" value="<c:out value="${student.id}"/>">
                            <button type="submit" class="btn-delete">删除</button>
                        </form>
                    </td>
                </tr>
            </c:forEach>
            <c:if test="${empty students}">
                <tr>
                    <td colspan="9">暂无学生数据</td>
                </tr>
            </c:if>
        </tbody>
    </table>
</div>

<script>
    function performSearch() {
        const studentId = document.getElementById('studentIdSearch').value;
        const studentName = document.getElementById('studentNameSearch').value;
        const className = document.getElementById('classNameSearch').value;

        let queryString = '';
        if (studentId) {
            queryString += "studentId=" + encodeURIComponent(studentId) + "&";
        }
        if (studentName) {
            queryString += "studentName=" + encodeURIComponent(studentName) + "&";
        }
        if (className) {
            queryString += "className=" + encodeURIComponent(className) + "&";
        }

        if (queryString.endsWith('&')) {
            queryString = queryString.slice(0, -1);
        }

        window.location.href = "guanliyuan_student_manage" + (queryString ? '?' + queryString : '');
    }
</script>
</body>
</html> 